package com.tanhua.server.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.server.service.IMService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("messages")
public class IMController {

    @Autowired
    private IMService imService;

    /**
     * 在消息模块中通过环信用户名查找用户信息
     *
     * @return
     */
    @GetMapping("userinfo")
    public ResponseEntity<UserInfoVo> findUserByHuanXinId(@RequestParam("huanxinId") String username) {
        try {
            UserInfoVo userInfoVo = imService.findUserByHuanXinId(username);
            if (ObjectUtil.isNotEmpty(userInfoVo)) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error("根据环信id查询用户信息! userName = " + username, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 添加好友
     *
     * @param param 想要添加为好友的用户ID
     * @return
     */
    @PostMapping("contacts")
    public ResponseEntity<Void> addFriend(@RequestBody Map<String, Object> param) {

        Long friendId = Long.valueOf(param.get("userId").toString());
        try {
            Boolean result = imService.addFriend(friendId);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("添加联系人失败! param = " + param, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 联系人列表
     *
     * @return
     */
    @GetMapping("contacts")
    public ResponseEntity<PageResult> findContactsList(@RequestParam(defaultValue = "1", value = "page") Integer page,
                                                       @RequestParam(defaultValue = "10", value = "pagesize") Integer pageSize,
                                                       @RequestParam(value = "keyword", required = false) String keyWord) {
        try {
            PageResult pageResult = imService.findContactsList(page, pageSize, keyWord);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("获取好友列表失败" + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息模块的点赞列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("likes")
    public ResponseEntity<PageResult> findLikeList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = imService.findLikeList(page, pageSize);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("获取点赞列表失败" + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息模块的喜欢列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("loves")
    public ResponseEntity<PageResult> findLoveList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = imService.findLoveList(page, pageSize);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("获取喜欢列表失败" + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息模块的评论列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("comments")
    public ResponseEntity<PageResult> findCommentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                      @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = imService.findCommentList(page, pageSize);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("获取评论列表失败" + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息模块的公告列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("announcements")
    @NoAuthorization
    public ResponseEntity<PageResult> findAnnouncementsList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = imService.findAnnouncementsList(page, pageSize);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("获取公告列表失败" + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
